package com.wd.cloud.applet.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wd.cloud.applet.common.constant.dataverify.Constant;
import com.wd.cloud.applet.common.exception.CustomException;
import com.wd.cloud.applet.config.CustomListResultConfig;
import com.wd.cloud.applet.config.ModelMapperConfig;
import com.wd.cloud.applet.config.MybatisPlusPageConfig;
import com.wd.cloud.applet.domain.ISysRoleMenuInfoDomain;
import com.wd.cloud.applet.entity.bo.SysRoleInfoBO.SysRoleInfoListBO;
import com.wd.cloud.applet.entity.bo.SysRoleInfoBO.SysRoleInfoSaveBO;
import com.wd.cloud.applet.entity.commons.RequestPrimaryKeyModel;
import com.wd.cloud.applet.entity.dto.SysRoleInfoModel;
import com.wd.cloud.applet.entity.dto.SysRoleMenuInfoModel;
import com.wd.cloud.applet.entity.vo.SysRoleInfoVO.SysRoleInfoKeyVO;
import com.wd.cloud.applet.entity.vo.SysRoleInfoVO.SysRoleInfoListVO;
import com.wd.cloud.applet.mapper.SysRoleInfoMapper;
import com.wd.cloud.applet.mapper.SysRoleMenuInfoMapper;
import com.wd.cloud.applet.service.ISysRoleInfoService;
import com.wd.cloud.applet.service.ISysRoleMenuInfoService;
import com.wd.cloud.applet.utils.HelperUtils;
import com.wd.cloud.applet.utils.string.StringUtils;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;

/**
* Description: [服务实现]
* Created on 2023年04月21日
* @author  <a href="mailto: 987707681@qq.com">hjx</a>
* @version 1.0
* Copyright (c) 2023年 代码生成器
*/
@Service
public class SysRoleInfoServiceImpl extends ServiceImpl<SysRoleInfoMapper, SysRoleInfoModel> implements ISysRoleInfoService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Resource
    private SysRoleInfoMapper appletMapper;
    @Resource
    private SysRoleMenuInfoMapper menuInfoMapper;
    @Autowired
    private ISysRoleMenuInfoDomain menuInfoDomain;
    /**
     * 根据主键ID删除数据（逻辑删除）
     *
     * @param params
     * @return
     */
    @Override
    public int deleteByPrimaryKey(RequestPrimaryKeyModel params) {
        if (StringUtils.isNullOrTrimEmpty(params.getId())) {
            throw new CustomException("请选择需要删除的数据", 100);
        }
        SysRoleInfoModel model = appletMapper.selectById(params.getId());
        if (model == null) {
            throw new CustomException("删除的数据不存在", 100);
        }
        model.setDeleteMark(Constant.SUCCESS_DELETE);
        appletMapper.deleteById(model);
        LambdaQueryWrapper<SysRoleMenuInfoModel> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(SysRoleMenuInfoModel::getRoleId,model.getId());
        return menuInfoMapper.delete(lambdaQuery);
    }

    /**
     * 新增数据
     *
     * @param params
     * @return
     */
    @Override
    public int insertSelective(SysRoleInfoSaveBO params) {
        SysRoleInfoModel model = ModelMapperConfig.mapTo(SysRoleInfoModel.class, params);
        model.setId(HelperUtils.getUuid());
        model.setCreateTime(new Date());
        return appletMapper.insert(model);
    }

    /**
     * 新增或修改
     *
     * @param params
     * @return
     */
    @Override
    public int save(SysRoleInfoSaveBO params) {
        SysRoleInfoModel model = ModelMapperConfig.mapTo(SysRoleInfoModel.class, params);
        List<SysRoleMenuInfoModel> roleMenusList = new ArrayList<>();
        if (model != null) {
            if (model.getId() == null || model.getId().isEmpty()) {
                model.setId(HelperUtils.getUuid());
                model.setCreateTime(new Date());
                appletMapper.insert(model);
                if (params.menu.length > 0) {
                    for (String str : params.menu) {
                        SysRoleMenuInfoModel menuInfoModel = new SysRoleMenuInfoModel();
                        menuInfoModel.setId(HelperUtils.getUuid());
                        menuInfoModel.setMenuId(str);
                        menuInfoModel.setRoleId(model.getId());
                        roleMenusList.add(menuInfoModel);
                    }
                    menuInfoDomain.saveBatch(roleMenusList);
                }
                return 1;
            } else {
                LambdaQueryWrapper<SysRoleMenuInfoModel> lambdaQuery = Wrappers.lambdaQuery();
                lambdaQuery.eq(SysRoleMenuInfoModel::getRoleId, model.getId());
                menuInfoMapper.delete(lambdaQuery);
                if (params.menu.length > 0) {
                    for (String str : params.menu) {
                        SysRoleMenuInfoModel menuInfoModel = new SysRoleMenuInfoModel();
                        menuInfoModel.setId(HelperUtils.getUuid());
                        menuInfoModel.setMenuId(str);
                        menuInfoModel.setRoleId(model.getId());
                        roleMenusList.add(menuInfoModel);
                    }
                    menuInfoDomain.saveBatch(roleMenusList);
                }
                return appletMapper.updateById(model);
            }
        }
        return 0;
    }

    /**
     * 分页获取数据列表
     *
     * @param params
     * @return
     */
    @Override
    public CustomListResultConfig<SysRoleInfoListVO> selectByPageList(SysRoleInfoListBO params) {
        LambdaQueryWrapper<SysRoleInfoModel> userLambdaQueryWrapper = Wrappers.lambdaQuery();
        userLambdaQueryWrapper.orderByDesc(SysRoleInfoModel::getCreateTime);
        Page<SysRoleInfoModel> page = new Page<>(params.getPage(), params.getLimit());
        IPage<SysRoleInfoModel> entityIPage = appletMapper.selectPage(page, userLambdaQueryWrapper);
        CustomListResultConfig<SysRoleInfoListVO> responseDtoList =
            MybatisPlusPageConfig.toPageResult(entityIPage, SysRoleInfoListVO.class);
        return responseDtoList;
    }
    /**
     * 分页获取数据列表
     *
     * @param params
     * @return
     */
    @Override
    public List<SysRoleInfoListVO> selectByList(SysRoleInfoListBO params) {
        LambdaQueryWrapper<SysRoleInfoModel> userLambdaQueryWrapper = Wrappers.lambdaQuery();
        userLambdaQueryWrapper.orderByDesc(SysRoleInfoModel::getCreateTime);
        List<SysRoleInfoModel> entityIPage = appletMapper.selectList(userLambdaQueryWrapper);
        List<SysRoleInfoListVO> responseDtoList =
            ModelMapperConfig.mapList(entityIPage, SysRoleInfoListVO.class);
        return responseDtoList;
    }

    /**
     * 根据主键id获取实体
     *
     * @param params
     * @return
     */
    @Override
    public SysRoleInfoKeyVO selectByPrimaryKey(RequestPrimaryKeyModel params) {
        if (params.getId() == null || params.getId().isEmpty()) {
            throw new CustomException("Id不能为空", 100);
        }
        SysRoleInfoModel model = appletMapper.selectById(params.getId());
        SysRoleInfoKeyVO responestDto = ModelMapperConfig.mapTo(SysRoleInfoKeyVO.class, model);
        if (responestDto == null) {
            throw new CustomException("Id错误", 100);
        }
        return responestDto;
    }

}
